#based on openCV camera.py example and http://www.aishack.in/2010/07/tracking-colored-objects-in-opencv/

import cv2.cv as cv
import time
from Tkinter import *   #
import copy

cv.NamedWindow("camera", 1)
cv.NamedWindow("mask", 1)
cv.NamedWindow("masked", 1)
capture = cv.CaptureFromCAM(1)


xMax=600
yMax=200



  


class Run:
    


    def setBack(self):
        # print self.imgNew,self.imgLast
        cv.Copy(self.imgNew,self.imgBack)
          

    def stepCam(self):
      
        self.imgNew = cv.QueryFrame(capture)
        
        # print id(imgNew)
        
        
        cv.AbsDiff(self.imgNew,self.imgBack,self.imgDiff)
       
        
        cv.CvtColor(self.imgDiff, self.imgHSV, cv.CV_BGR2HSV)
       
        cv.InRangeS(self.imgHSV, cv.Scalar(0, 0, 30), cv.Scalar(255, 255, 255), self.imgThreshed)
        #cv.ShowImage("camera", self.imgThreshed)
        #cv.ShowImage("threshed", imgThreshed)
        #moments = cv.Moments(cv.GetMat(imgThreshed))
        cv.Copy(self.imgNew,self.imgDiff,self.imgThreshed)
        
        cv.ShowImage("camera", cv.GetMat(self.imgNew))
        cv.ShowImage("mask", cv.GetMat(self.imgThreshed))
        cv.ShowImage("masked", cv.GetMat(self.imgDiff))
        
        if cv.WaitKey(10) == 27:
            run.quit()
       

    def __init__(self):
       
        #P.ion()
        #P.xlabel("Evaluations")
        #P.ylabel("Fitness")
       
        self.running = False
        self.first = True
        master = Tk()
        self.frame=Frame(master)
        self.frame.pack()
        c=0
        b = Button(self.frame, text="RUN", fg="black", command=self.run)
        b.grid(row=0,column=c)
        c+=1
        
        b = Button(self.frame, text="BACK ", fg="black", command=self.setBack)
        b.grid(row=0,column=c)
        c+=1
     
     
     
        b = Button(self.frame, text="QUIT ", fg="black", command=self.quit)
        b.grid(row=0,column=c)
        c+=1
     
        self.canvas = Canvas(self.frame, width=xMax, height=yMax+40)
        self.canvas.grid(row=1,columnspan=c)
                  
        self.imgNew = cv.QueryFrame(capture)
      
        # print id(imgNew)
        
        self.imgBack = cv.CreateImage(cv.GetSize(self.imgNew), 8, 3)
        self.imgDiff = cv.CreateImage(cv.GetSize(self.imgNew), 8, 3)
        self.imgHSV = cv.CreateImage(cv.GetSize(self.imgNew), 8, 3)        
        self.imgAbs = cv.CreateImage(cv.GetSize(self.imgNew), 8, 3) 
        self.imgThreshed = cv.CreateImage(cv.GetSize(self.imgNew), 8, 1)
  
    def quit(self):
        self.running =False
        cv.DestroyWindow("camera")
        cv.DestroyWindow("threshed")
        
        
    def run(self):
        self.running =True
        while self.running:   # ] self.count < MAXITER and self.running:
            self.step();
           

    def step(self):
        
        self.stepCam()
        # give the GUI a chance to do stuff  
        self.frame.update()
        
      
        
if __name__ == '__main__':

    run=Run()
    mainloop() 
